package com.dragon.mmochat.model.deal;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyDealState;
import com.dragon.mmochat.model.enumType.MmochatBigMoneyDealType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

//元宝交易
public class MmochatBigMoneyDeal implements Comparable<MmochatBigMoneyDeal> {
	private int id; // 订单号
	private MmochatBigMoneyDealType type; // 交易类型
	private MmochatBigMoneyDealState state; // 交易状态
	private int price; // 单价(万/100元宝)
	private int amount; // 元宝总量
	private int rest; // 尚未成交的元宝量
	private long startTick; // 创建时间
	private int roleId; // 订单发布人roleId
	// 交易表：交易者roleId-->成交的元宝量
	private Map<Integer, Integer> dealerMap = new ConcurrentHashMap<Integer, Integer>();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MmochatBigMoneyDealType getType() {
		return type;
	}

	public void setType(MmochatBigMoneyDealType type) {
		this.type = type;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public long getStartTick() {
		return startTick;
	}

	public void setStartTick(long startTick) {
		this.startTick = startTick;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public int getRest() {
		return rest;
	}

	public void setRest(int rest) {
		this.rest = rest;
	}

	public void delRest(int del) {
		rest -= del;
		rest = Math.max(0, rest);
	}

	public boolean isOver() {
		return rest <= 0;
	}

	public MmochatBigMoneyDealState getState() {
		return state;
	}

	public void setState(MmochatBigMoneyDealState state) {
		this.state = state;
	}

	public Map<Integer, Integer> getDealerMap() {
		return dealerMap;
	}

	public void setDealerMap(Map<Integer, Integer> dealerMap) {
		this.dealerMap = new ConcurrentHashMap<Integer, Integer>(dealerMap);
	}

	public String getGson_dealerMap() {
		if (dealerMap == null) {
			return null;
		}
		Gson gson = new Gson();
		return gson.toJson(dealerMap, new TypeToken<Map<Integer, Integer>>() {
		}.getType());
	}

	// 是否有效
	public boolean isValid() {
		return System.currentTimeMillis() - startTick < MmochatConstant.bigmoneyDealOrderLifeTime;
	}

	// 按单价排序，寄售的元宝价格低的排前面；求购的元宝价格高的排前面；如果价格相同，则发布时间早的排前面
	// 寄售排求购前面
	@Override
	public int compareTo(MmochatBigMoneyDeal p2) {
		if (type != p2.getType()) {
			// 寄售排求购前面
			if (type == MmochatBigMoneyDealType.寄售) {
				return 0;
			} else {
				return 1;
			}
		} else if (type == MmochatBigMoneyDealType.寄售) {
			// 都是寄售，价低排前面
			if (price != p2.getPrice()) {
				return price - p2.getPrice();
			} else {
				// 价格相同，时间早的排前面
				return (int) (startTick - p2.getStartTick());
			}
		} else {
			// 都是求购，价高排前面
			if (price != p2.getPrice()) {
				return p2.getPrice() - price;
			} else {
				// 价格相同，时间早的排前面
				return (int) (startTick - p2.getStartTick());
			}
		}
	}
}
